package com.itboye.sunsun.adapter;

import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.LunTanHomePageBean.LunTanItemBean;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.luntan.LunTanDetailActivity;
import com.itboye.sunsun.utils.DebugToast;
import com.itboye.sunsun.utils.XImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LunTanHomePageAdapter extends BaseAdapter {

	List<LunTanItemBean> dataList;
	Activity activity;

	public LunTanHomePageAdapter(List<LunTanItemBean> dataList,
			Activity activity) {
		// TODO Auto-generated constructor stub
		this.dataList = dataList;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(App.ctx).inflate(
					R.layout.item_homepage_luntan, null);
			holder = new ViewHolder();
			holder.bankuai = (TextView) convertView.findViewById(R.id.bankuai);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.zan = (TextView) convertView.findViewById(R.id.zan);
			holder.commont = (TextView) convertView.findViewById(R.id.commont);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.head = (ImageView) convertView.findViewById(R.id.head);
			holder.iv1 = (ImageView) convertView.findViewById(R.id.iv1);
			holder.iv2 = (ImageView) convertView.findViewById(R.id.iv2);
			holder.iv3 = (ImageView) convertView.findViewById(R.id.iv3);

			holder.jinru = (ViewGroup) convertView.findViewById(R.id.jinru);
			holder.imgsContainer = (ViewGroup) convertView
					.findViewById(R.id.imgsContainer);
			holder.jinru.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					LunTanItemBean bean = dataList.get((Integer) v.getTag());
					Intent intent = new Intent(activity,
							LunTanDetailActivity.class);
					intent.putExtra("fid", bean.getFid());
					intent.putExtra("title", bean.getName());
					activity.startActivity(intent);

				}
			});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		LunTanItemBean bean = dataList.get(position);
		holder.bankuai.setText(bean.getName());
		if (bean.getUserInfo() == null) {
			holder.name.setText(bean.getAuthor());
		} else {
			holder.name.setText(bean.getUserInfo().getNickname());
		}
		System.out.println("bean.getSubject()"+bean.getSubject()+"\ngetMessage"+bean.getMessage());
		holder.title.setText(bean.getSubject());
		holder.content.setText(bean.getMessage());
		holder.time.setText(bean.getDatelineShow());
		holder.zan.setText(bean.getLikes());
		holder.commont.setText(bean.getReplies());

		List<String> imgs = bean.getImg();
		if (imgs != null && imgs.size() > 0) {
			holder.iv1.setVisibility(View.VISIBLE);
			holder.iv2.setVisibility(View.VISIBLE);
			holder.iv3.setVisibility(View.VISIBLE);

			if (imgs.size() > 0) {
				XImageLoader.load(NetPublicConstant.IMAGE_URL + imgs.get(0),
						holder.iv1);
			} else {
				holder.iv2.setImageBitmap(null);
				holder.iv3.setImageBitmap(null);
			}

			if (imgs.size() > 1) {
				XImageLoader.load(NetPublicConstant.IMAGE_URL + imgs.get(1),
						holder.iv2);
			} else {
				holder.iv3.setImageBitmap(null);
			}

			if (imgs.size() > 2) {
				XImageLoader.load(NetPublicConstant.IMAGE_URL + imgs.get(2),
						holder.iv3);
			}
		} else {
			holder.iv1.setVisibility(View.GONE);
			holder.iv2.setVisibility(View.GONE);
			holder.iv3.setVisibility(View.GONE);
		}

		holder.jinru.setTag(position);
		try {
			if (bean.getUserInfo().getHead() == ""
					|| bean.getUserInfo().getHead() == null) {
				Bitmap bitmap = BitmapFactory.decodeResource(
						activity.getResources(), R.drawable.defaultimage);
				holder.head.setImageBitmap(bitmap);
			} else {
				holder.head.setImageBitmap(null);
				XImageLoader.load(NetPublicConstant.IMAGE_URL
						+ bean.getUserInfo().getHead(), holder.head);
			}
		} catch (Exception e) {
		}
		return convertView;
	}

	class ViewHolder {

		TextView title;
		TextView bankuai;
		TextView name;
		TextView content;
		TextView time;
		TextView zan;
		TextView commont;
		ImageView head;

		ImageView iv1;
		ImageView iv2;
		ImageView iv3;

		ViewGroup jinru;
		ViewGroup imgsContainer;
	}
}
